// pages/settlement/settlement.js
const app = getApp()
var util = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [{
        "searchValue": null,
        "createBy": null,
        "createTime": "2020-04-28 10:19:11",
        "updateBy": null,
        "updateTime": "2020-04-28 13:03:17",
        "remark": null,
        "dataScope": null,
        "typeId": 59,
        "typeDetailId": null,
        "cityId": null,
        "ancestors": null,
        "typeName": "工人服务",
        "sysModel": "1",
        "parentTypeId": null,
        "level": null,
        "typeUrl": "/profile/upload/2020/04/28/23b0038ab48a76a8492101e8962ec2c3.jpg",
        "userId": null,
        "status": "0",
        "delFlag": null,
        "sort": null,
        "sysTypeDetail": null,
        "children": []
      },
      {
        "searchValue": null,
        "createBy": null,
        "createTime": "2020-04-28 13:22:11",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "dataScope": null,
        "typeId": 65,
        "typeDetailId": null,
        "cityId": null,
        "ancestors": null,
        "typeName": "家政服务",
        "sysModel": "0",
        "parentTypeId": null,
        "level": null,
        "typeUrl": "/profile/upload/2020/04/28/f25376fef4cdf66b0886826167ed27f7.jpg",
        "userId": null,
        "status": "0",
        "delFlag": null,
        "sort": null,
        "sysTypeDetail": null,
        "children": []
      },
      {
        "searchValue": null,
        "createBy": null,
        "createTime": "2020-04-28 13:22:11",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "dataScope": null,
        "typeId": 65,
        "typeDetailId": null,
        "cityId": null,
        "ancestors": null,
        "typeName": "家政服务",
        "sysModel": "0",
        "parentTypeId": null,
        "level": null,
        "typeUrl": "/profile/upload/2020/04/28/f25376fef4cdf66b0886826167ed27f7.jpg",
        "userId": null,
        "status": "0",
        "delFlag": null,
        "sort": null,
        "sysTypeDetail": null,
        "children": []
      },
      {
        "searchValue": null,
        "createBy": null,
        "createTime": "2020-04-28 13:22:11",
        "updateBy": null,
        "updateTime": null,
        "remark": null,
        "dataScope": null,
        "typeId": 65,
        "typeDetailId": null,
        "cityId": null,
        "ancestors": null,
        "typeName": "家政服务",
        "sysModel": "0",
        "parentTypeId": null,
        "level": null,
        "typeUrl": "/profile/upload/2020/04/28/f25376fef4cdf66b0886826167ed27f7.jpg",
        "userId": null,
        "status": "0",
        "delFlag": null,
        "sort": null,
        "sysTypeDetail": null,
        "children": []
      }
    ], //选择您的工种
    activeIndex: null, //激活下标
    addForm: {
      userName: '',
      backImage: '', //身份证背面图片
      frontImage: '', //身份证前面图片
      lawyerImage: '' //律师证
    },
    api: util.config().api + '/mobileUser/common/upload/',
    fileListfrontImage: [],
    fileListbackImage: [],
    fileListlawyerImage: [],


    show: false, //展示压屏
  },
  // 点击类型
  clickType(e) {
    // console.log(e);
    var index = e.currentTarget.dataset.index
    this.setData({
      activeIndex: index
    })
  },



  // 删除 身份证 前面
  deleteReadfrontImage() {
    this.setData({
      fileListfrontImage: [],
      'addForm.frontImage': ''
    })
  },
  // 删除 身份证 背面
  deleteReadbackImage() {
    this.setData({
      fileListbackImage: [],
      'addForm.backImage': ''
    })
  },
  // 删除 律师证
  deleteReadlawyerImage() {
    this.setData({
      fileListlawyerImage: [],
      'addForm.lawyerImage': ''
    })
  },
  // 上传前校验
  beforeRead(event) {
    const {
      file,
      callback
    } = event.detail;
    callback(file.type === 'image');
  },
  // 上传后校验    身份证 前面
  afterReadfrontImage(event) {
    let that = this
    const {
      file
    } = event.detail;
    console.log(file);

    wx.uploadFile({
      url: that.data.api, // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        // 上传完成需要更新 fileList
        var data = JSON.parse(res.data)
        console.log('图片上传服务器返回值', data)
        var fileList = []
        fileList.push(data)
        that.setData({
          fileListfrontImage: fileList,
          'addForm.frontImage': data.fileName
        });

      },
    });

    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    // wx.uploadFile({
    //   url: that.data.api,
    //   success(res) {
    //     const data = res
    //     console.log(res);

    //   }
    // })
  },
  // 上传后校验    身份证 背面
  afterReadbackImage(event) {
    let that = this
    const {
      file
    } = event.detail;
    console.log(file);

    wx.uploadFile({
      url: that.data.api, // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        // 上传完成需要更新 fileList
        var data = JSON.parse(res.data)
        console.log('图片上传服务器返回值', data)
        var fileList = []
        fileList.push(data)
        that.setData({
          fileListbackImage: fileList,
          'addForm.backImage': data.fileName
        });
      },
    });
  },
  // 上传后校验    律师证
  afterReadlawyerImage(event) {
    let that = this
    const {
      file
    } = event.detail;
    console.log(file);

    wx.uploadFile({
      url: that.data.api, // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        // 上传完成需要更新 fileList
        var data = JSON.parse(res.data)
        console.log('图片上传服务器返回值', data)
        var fileList = []
        fileList.push(data)
        that.setData({
          fileListlawyerImage: fileList,
          'addForm.lawyerImage': data.fileName
        });
      },
    });
  },

  // 底部立即提交
  submitNow() {
    this.setData({
      show: true
    })
  },
  // 压屏关闭
  onClickHide() {
    this.setData({
      show: false
    });
  },
  //监听值变化
  onChange(e) {
    var formName = e.target.dataset.formname
    var name = `addForm.${formName}`;
    this.setData({
      [name]: e.detail
    })
    console.log(formName)
  },

  //跳转
  navigateTo(e) {
    const data = e.currentTarget.dataset.data //跳转参数
    const path = e.currentTarget.dataset.path

    wx.navigateTo({
      url: path,
    })

    this.onClickHide()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})